﻿package com.tudou.track
{
    import com.tudou.config.*;
    import com.tudou.model.*;
    import com.tudou.newPlayer.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class PlayTimeTracker extends Object
    {
        private var timer:Timer;
        private var interval:Number;
        private var url:String;
        private var playerStatus:IPlayerStatus;
        private var baseInfo:IBaseInfo;
        private static var lock:Boolean = true;
        private static var instance:PlayTimeTracker = null;

        public function PlayTimeTracker()
        {
            if (!lock)
            {
            }
            else
            {
                throw new Error("This is a singleton class CountPlayTime!");
            }
            return;
        }// end function

        public function start(param1:IPlayerStatus, param2:IBaseInfo) : void
        {
            if (param1 == null || param2 == null)
            {
                return;
            }
            playerStatus = param1;
            baseInfo = param2;
            interval = Config.COUNT_PLAYTIME_INTERVAL;
            url = Config.COUNT_PLAYTIME_URL;
            timer = new Timer(Math.random() * interval);
            timer.addEventListener(TimerEvent.TIMER, timerHlr);
            timer.start();
            return;
        }// end function

        public function stop() : void
        {
            if (timer)
            {
                timer.stop();
                timer.removeEventListener(TimerEvent.TIMER, timerHlr);
            }
            timer = null;
            playerStatus = null;
            return;
        }// end function

        private function timerHlr(event:TimerEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_3:String = null;
            if (timer.delay != interval)
            {
                timer.delay = interval;
            }
            if (playerStatus.isPlaying)
            {
                _loc_2 = [baseInfo.iid, int(playerStatus.playTime), baseInfo.juid];
                _loc_3 = _loc_2.join("|");
                sendToURL(new URLRequest(url + "?v=1&s=" + _loc_3));
            }
            return;
        }// end function

        public function get isRunning() : Boolean
        {
            if (timer)
            {
                return timer.running;
            }
            return false;
        }// end function

        public static function getInst() : PlayTimeTracker
        {
            if (instance == null)
            {
                lock = false;
                instance = new PlayTimeTracker;
                lock = true;
            }
            return instance;
        }// end function

    }
}
